// userPageC/pages/userPersonal/evaluate/evaluate.js
const app = getApp()
const WXAPI = require("../../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '评价', // 导航栏 中间的标题
      textColor: '#000', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isColor: true,
      isScrollY: '',
      isShare: '' //是否来自分享 0 :不是，1：是
    },
    imgHttp: app.globalData.imgHttp, //图片前缀
    starMap: ['1','2','3','4','5',],//店铺环境评价数组
    serviceStarMap:['1','2','3','4','5',],//服务态度评价数组
    textLength:0,//统计字数
    imgList:[],//图片数组
    switchChecked: true,//是否匿名
    orderId: '',
    environmentPoints: 0,
    servicePoints:0,
    content: '',
    showUpload: true,
    isSumit: true,//是否可以继续提交（防重复提交）
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var orderId = options.orderId
    console.log('orderId',orderId)
    this.setData({
      orderId: orderId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  shopStarChoose(e) { //店铺环境点击评价
    console.log(e.target.dataset.star)
    
    let environmentPoints = parseInt(e.target.dataset.star) || 0;
    if(this.data.environmentPoints!=environmentPoints){
      this.setData({
        environmentPoints: environmentPoints,
      });
    }
    console.log('this.data.environmentPoints',this.data.environmentPoints)
  },
  serviceStarChoose(e) { //服务态度点击评价
    let servicePoints = parseInt(e.target.dataset.star) || 0;
    if(this.data.servicePoints!=servicePoints){
      this.setData({
        servicePoints: servicePoints 
      });
    }
  },
  bindinput(e){ //监听输入框内容
    console.log(e.detail.value)
    let value = e.detail.value
    this.setData({
      content: value,
      textLength: value.length
    })
  },
  uploadImg: function (e) { //图片上传
    const that = this;
    var count = 0
    count = 9 - this.data.imgList.length 
    wx.chooseMedia({
      count: count, // 默认9,
      mediaType: ['image'],
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        let tempFilePaths = res.tempFiles;
        let uploaderList = tempFilePaths
        for (var i = 0; i < uploaderList.length; i++) {
          var data = {
            file: uploaderList[i].tempFilePath
          }
          WXAPI.appUploadFile(data, (res) => {
            console.log("上传图片", res.result.url)
            if (res.code === 200) {
              that.data.imgList.push(res.result.url)
              that.setData({
                imgList: that.data.imgList,
              })
              if(that.data.imgList.length >= 8){
                that.setData({
                  showUpload: false,
                })
              }else{
                that.setData({
                  showUpload: true,
                })
              }
            }
          })
        }
      }
    })
  },
  showImg: function (e) {//展示图片
    var that = this;
    let imglists = that.data.imgList.map((item, index) => {
      return that.data.imgHttp + item
    })
    wx.previewImage({
      urls: imglists,
      current: that.data.imgList[e.currentTarget.dataset.index]
    })
  },
  clearImg(e){ //删除图片
    let index = e.currentTarget.dataset.index
    this.data.imgList.splice(index, 1);
    this.setData({
      imgList: this.data.imgList
    })
    if(this.data.imgList.length >= 8){
      this.setData({
        showUpload: false,
      })
    }else{
      this.setData({
        showUpload: true,
      })
    }
  },
  switchChange(e){ //监听是否匿名开关选择
    console.log(e.detail.value)
    this.setData({
      switchChecked: e.detail.value
    })
  },
  submit(){
    if(this.data.environmentPoints < 1){
      wx.showToast({
        title: '请选择环境评分',
        icon: 'none'
      })
      return
    }else if(this.data.servicePoints < 1){
      wx.showToast({
        title: '请选择态度评分',
        icon: 'none'
      })
      return
    }
    this.addSubmit()
  },
  addSubmit(){
    if (!this.data.isSumit) {
      console.log('重复提交')
      return
    }
    this.data.isSumit = false
    wx.showLoading({
      title: '正在提交',
      mask: true,
    })
    var data ={
      orderId: this.data.orderId,
      environmentPoints: this.data.environmentPoints,
      servicePoints: this.data.servicePoints,
      content: this.data.content,
      image: JSON.stringify(this.data.imgList),
      anonymousType: this.data.switchChecked ? 2 : 1
    }
    console.log('提交数据',data)
    WXAPI.addEvaluate(data,(res) => {
      if (res.code==200) {
        wx.showToast({
          title: '提交成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.hideLoading({})
          this.getRefresh()
          wx.navigateBack({
            delta: 1,
          })
          this.data.isSumit = true
        }, 500);
      }else{
        this.data.isSumit = true
      }
    })
  },
  //评价后刷新
  getRefresh(){
    var pages = getCurrentPages();
        // 上一页面栈
        var prevPage = pages[pages.length - 2]; 
        // 上一页面跳转携带的参数
        console.log(prevPage.options); 
        prevPage.setData({
          name: prevPage.options.name,
          id: prevPage.options.id,
          orderList: [],
          pageNum: 1,
        })
        prevPage.getList()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})